﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_CGJH : Form
    {
        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;
        
        public Form_CGJH()
        {
            InitializeComponent();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            Form_CGSQD CGSQD = new Form_CGSQD();
            CGSQD.ShowDialog();
            refresh();
        }

        private void bindingNavigatorEnterItem_Click(object sender, EventArgs e)
        {
            if(dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex] != null)
            { 
                Form_CGSQD CGSQD = new Form_CGSQD(Convert.ToInt32((dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex].Cells[0].Value)));
                CGSQD.ShowDialog();
            }
        }

        private void Form_CGJH_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            sda = new SqlDataAdapter("select CGJH0.CGDH, CGJH0.RQ, GYS.MC, RY.MC, RY1.MC,CASE CGJH0.Hassh WHEN 0 THEN '未审核' ELSE '已审核' END AS STATE " +
                                     "from  ML_CGJH0 CGJH0 LEFT JOIN Dic_GYS GYS on GYS.BM = CGJH0.GYSBM LEFT JOIN Dic_RY RY ON CGJH0.LRRY = RY.BM LEFT JOIN Dic_RY RY1 ON CGJH0.SHRY = RY.BM " +   
                                     "where CGJH0.YKBM = '"+PublicUnit.UserKSBM +"' order BY CGJH0.CGDH", con);
            ds = new DataSet();
            try
            {
                sda.Fill(ds);
                dgvCGJH.DataSource = ds.Tables[0];
                dgvCGJH.Columns[0].HeaderText = "单号";
                dgvCGJH.Columns[1].HeaderText = "制单日期";
                dgvCGJH.Columns[2].HeaderText = "供应商";
                dgvCGJH.Columns[3].HeaderText = "录入";
                dgvCGJH.Columns[4].HeaderText = "审核";
                dgvCGJH.Columns[5].HeaderText = "状态";
                dgvCGJH.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvCGJH.SelectedRows[0].Index - 1 + dgvCGJH.RowCount) % dgvCGJH.RowCount;
            dgvCGJH.ClearSelection();
            dgvCGJH.Rows[rowindx].Selected = true;
            dgvCGJH.FirstDisplayedScrollingRowIndex = dgvCGJH.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvCGJH.SelectedRows[0].Index + 1) % dgvCGJH.RowCount;
            dgvCGJH.ClearSelection();
            dgvCGJH.Rows[rowIndex].Selected = true;
            dgvCGJH.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvCGJH.FirstDisplayedScrollingRowIndex)
            {
                dgvCGJH.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvCGJH.Rows[rowIndex].Displayed)
            {
                dgvCGJH.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvCGJH.ClearSelection();
            dgvCGJH.Rows[0].Selected = true;
            dgvCGJH.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvCGJH.ClearSelection();
            dgvCGJH.Rows[dgvCGJH.RowCount - 2].Selected = true;
            dgvCGJH.FirstDisplayedScrollingRowIndex = dgvCGJH.RowCount - 1;

        }


        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("确认删除？","警告",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning) == DialogResult.OK)
            {
                try
                {
                    using (SqlConnection con = new SqlConnection(PublicUnit.ConnetionString))
                    {
                        con.Open();
                        SqlCommand com = con.CreateCommand();
                        SqlTransaction tran;
                        tran = con.BeginTransaction();
                        com.Connection = con;
                        com.Transaction = tran;
                        try
                        {
                            com.CommandText = "delete from ML_CGJH1 where cgdh = '" + dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex].Cells[0].Value.ToString().Trim() + "'" ;
                            com.ExecuteNonQuery();
                            com.CommandText = "delete from ML_CGJH0 where cgdh = '" + dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex].Cells[0].Value.ToString().Trim() + "'";
                            com.ExecuteNonQuery();
                        }
                        catch(Exception)
                        {
                            tran.Rollback();
                        }
                    }
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 18456)
                        MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            refresh();
            }
        }

        private void bindingNavigatorRefreshItem_Click(object sender, EventArgs e)
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            sda = new SqlDataAdapter("select CGJH0.CGDH, CGJH0.RQ, GYS.MC, RY.MC, RY1.MC,CASE CGJH0.HasSH WHEN 0 THEN '未审核' ELSE '已审核' END AS STATE " +
                                     "from  ML_CGJH0 CGJH0 LEFT JOIN Dic_GYS GYS on GYS.BM = CGJH0.GYSBM LEFT JOIN Dic_RY RY ON CGJH0.LRRY = RY.BM LEFT JOIN Dic_RY RY1 ON CGJH0.SHRY = RY.BM " +
                                     "where CGJH0.YKBM = '" + PublicUnit.UserKSBM + "' and CGJH0.RQ Between '"+ dtpBegin.ToString() + "' and '" + dtpEnd.ToString() + "' ", con);
            if (cbxWSH.Checked == true)
                sda.SelectCommand.CommandText += "and CGJH0.HasSH = 0 ";
            if(cbxYSH.Checked == true)
                sda.SelectCommand.CommandText += "and CGJH0.HasSH = 1 ";
            sda.SelectCommand.CommandText += "order BY CGJH0.CGDH";
            ds = new DataSet();
            try
            {
                sda.Fill(ds);
                dgvCGJH.DataSource = ds.Tables[0];
                dgvCGJH.Columns[0].HeaderText = "单号";
                dgvCGJH.Columns[1].HeaderText = "制单日期";
                dgvCGJH.Columns[2].HeaderText = "供应商";
                dgvCGJH.Columns[3].HeaderText = "录入";
                dgvCGJH.Columns[4].HeaderText = "审核";
                dgvCGJH.Columns[5].HeaderText = "状态";
                dgvCGJH.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tsbSH_Click(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(PublicUnit.ConnetionString))
                {
                    SqlCommand com = new SqlCommand("update ML_CGJH0 set HasSH = 1 and SHRY = '"+ PublicUnit.UserID +"' where cgdh = '"+dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex].Cells[0].Value.ToString() +"' ",con);
                    con.Open();
                    if (com.ExecuteNonQuery() == 1)
                        MessageBox.Show("删除成功","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            catch(SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            refresh();
        }

        private void refresh()
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            sda = new SqlDataAdapter("select CGJH0.CGDH, CGJH0.RQ, GYS.MC, RY.MC, RY1.MC,CASE CGJH0.Hassh WHEN 0 THEN '未审核' ELSE '已审核' END AS STATE " +
                                     "from  ML_CGJH0 CGJH0 LEFT JOIN Dic_GYS GYS on GYS.BM = CGJH0.GYSBM LEFT JOIN Dic_RY RY ON CGJH0.LRRY = RY.BM LEFT JOIN Dic_RY RY1 ON CGJH0.SHRY = RY.BM " +
                                     "where CGJH0.YKBM = '" + PublicUnit.UserKSBM + "' order BY CGJH0.CGDH", con);
            ds = new DataSet();
            try
            {
                sda.Fill(ds);
                dgvCGJH.DataSource = ds.Tables[0];
                dgvCGJH.Columns[0].HeaderText = "单号";
                dgvCGJH.Columns[1].HeaderText = "制单日期";
                dgvCGJH.Columns[2].HeaderText = "供应商";
                dgvCGJH.Columns[3].HeaderText = "录入";
                dgvCGJH.Columns[4].HeaderText = "审核";
                dgvCGJH.Columns[5].HeaderText = "状态";
                dgvCGJH.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgvCGJH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvCGJH.Rows[dgvCGJH.CurrentCell.RowIndex].Cells[5].ToString() == "已审核")
            {
                tsbSH.Enabled = false;
                bindingNavigatorDeleteItem.Enabled = false;
            }
            else
            {
                tsbSH.Enabled = true;
                bindingNavigatorDeleteItem.Enabled = true;
            }
        }

    }
}
